#include "widget.h"
#include "ui_widget.h"
#include "QSerialPortInfo"   //包含串口头文件
#include "QMessageBox"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QStringList serialNamePort;             //串口字符列表
    serialPort = new QSerialPort(this);     //实例化对象
    connect(serialPort,SIGNAL(readyRead()),this,SLOT(plainTextEdit_SerialInputData_ReadyData_Slot()));
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) {   //获取串口信息
        serialNamePort<<info.portName();    //将信息打印到列表中
    }
    ui->SerialCom->addItems(serialNamePort);//将列表中的数据填充到ui的控件中
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_Button_Serial_Open_clicked()
{
    QSerialPort::BaudRate baudrate_arr[] = {QSerialPort::Baud2400,QSerialPort::Baud2400,QSerialPort::Baud4800,QSerialPort::Baud9600,QSerialPort::Baud19200,QSerialPort::Baud38400,QSerialPort::Baud57600,QSerialPort::Baud115200};
    QSerialPort::DataBits databits_arr[] = {QSerialPort::Data5,QSerialPort::Data6,QSerialPort::Data7,QSerialPort::Data8};
    QSerialPort::StopBits stopbits_arr[] = {QSerialPort::OneStop,QSerialPort::OneAndHalfStop,QSerialPort::TwoStop};
    QSerialPort::Parity   parity_arr[]   = {QSerialPort::NoParity,QSerialPort::EvenParity,QSerialPort::OddParity};
    QSerialPort::BaudRate baudrate;
    QSerialPort::DataBits databits;
    QSerialPort::StopBits stopbits;
    QSerialPort::Parity   parity;

    baudrate = baudrate_arr[ui->SerialBaud->currentIndex()];        //获取窗口串口波特率
    databits = databits_arr[ui->SerialData_Bits->currentIndex()];   //获取数据位
    stopbits = stopbits_arr[ui->SerialStop_Bits->currentIndex()];   //获取停止位
    parity   = parity_arr[ui->Serial_Parity->currentIndex()];       //获取奇偶校验位
    serialPort->setPortName(ui->SerialCom->currentText());          //获取串口号
    serialPort->setBaudRate(baudrate);                              //获取波特率
    serialPort->setDataBits(databits);                              //设置数据位
    serialPort->setStopBits(stopbits);                              //设置停止位
    serialPort->setParity(parity);                                  //设置奇偶校验位

    if(serialPort->open(QIODevice::ReadWrite) == true){             //获取串口打开状态
        QMessageBox::information(this,"提示","成功");                //使用MessaBox的形式进行交互显示
    }else{
        QMessageBox::critical(this,"提示","失败");                    //使用MessaBox的形式进行交互显示
    }

}

void Widget::on_Button_Serial_Close_clicked()
{
    serialPort->close();                                            //关闭串口
}
void Widget::plainTextEdit_SerialInputData_ReadyData_Slot()
{
    QString buff;                                                   //定义数据流
    buff = QString(serialPort->readAll());                          //读取数据
    ui->plainTextEdit_SerialInputData->appendPlainText(buff);       //显示数据
}

void Widget::on_Button_Serial_SendData_clicked()
{
    serialPort->write(ui->LineEdit_Serial_InputData->text().toLocal8Bit().data());  //将数据转换成char类型的
}

void Widget::on_Button_Serial_CleanData_clicked()
{
    ui->plainTextEdit_SerialInputData->clear();                     //清除串口窗口显示信息
}
